package com.ct.controller.system;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ct.application.system.model.po.AdminSystemSavePO;
import com.ct.application.system.model.po.AdminSystemSearchPO;
import com.ct.application.system.model.po.AdminSystemUserSavePO;
import com.ct.application.system.model.vo.AdminSystemLoginVO;
import com.ct.application.system.model.vo.AdminSystemVO;
import com.ct.application.system.service.IAdminSystemService;
import com.ct.application.system.service.IAdminSystemUserService;
import com.ct.commons.BaseController;
import com.ct.commons.annotation.ApiLog;
import com.ct.commons.annotation.HasAdmin;
import com.ct.commons.annotation.PermissionApi;
import com.ct.commons.aspect.limiter.AccessIpLimiter;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 后台管理-系统管理
 * @author Auto Generator
 * @since 2023-08-16
 */
@RestController
@PermissionApi(title = "系统管理模块",path="/api/v1/system",code = "L1000",sorted = 13)
public class AdminSystemController extends BaseController {

    @Autowired
    private IAdminSystemService adminSystemService;
    @Autowired
    private IAdminSystemUserService adminSystemUserService;

    /**
     * 查询系统列表--用于用户绑定
     *
     * @param adminSystemSearchPO
     * @return
     */
    @PostMapping("/system/bind/list")
    @PermissionApi(title = "查询系统列表--用于用户绑定",path="/api/v1/system/bind/list",code = "L1001",sorted = 1)
    public Page<AdminSystemVO> findAdminSystemBind(@Validated @RequestBody AdminSystemSearchPO adminSystemSearchPO) {
        return adminSystemService.findAdminSystemBind(adminSystemSearchPO);
    }

    /**
     * 查询系统列表
     *
     * @param adminSystemSearchPO
     * @return
     */
    @PostMapping("/system/list")
    @PermissionApi(title = "查询系统列表",path="/api/v1/system/list",code = "L1002",sorted = 2)
    public Page<AdminSystemVO> findAdminSystem(@Validated @RequestBody AdminSystemSearchPO adminSystemSearchPO) {
        return adminSystemService.findAdminSystem(adminSystemSearchPO);
    }

    /**
     * 保存和更新
     *
     * @param adminSystemSavePO
     * @return
     */
    @PostMapping("/system/saveupdate")
    @ApiLog("系统保存和更新")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "系统保存和更新",path="/api/v1/system/saveupdate",code = "L1003",sorted = 3)
    public Boolean saveUpdateAdminSystem(@Validated @RequestBody AdminSystemSavePO adminSystemSavePO) {
        return adminSystemService.saveUpdateAdminSystem(adminSystemSavePO);
    }

    /**
     * 根据id查询系统
     *
     * @param id
     * @return
     */
    @PostMapping("/system/get/{id}")
    @PermissionApi(title = "根据id查询系统",path="/api/v1/system/get/{id}",code = "L1004",sorted = 4)
    public AdminSystemVO getAdminSystemById(@PathVariable("id") Long id) {
        return adminSystemService.getAdminSystemById(id);
    }

    /**
     * 根据id删除系统
     *
     * @param id
     * @return
     */
    @PostMapping("/system/del/{id}")
    @ApiLog("根据id删除系统")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "根据id删除系统",path="/api/v1/system/del/{id}",code = "L1005",sorted = 5)
    public Boolean delAdminSystemById(@PathVariable("id") Long id) {
        return adminSystemService.deleteAdminSystemById(id);
    }

    /**
     * 修改状态
     *
     * @return
     */
    @PostMapping("/system/status/{id}/{field}/{value}")
    @ApiLog("系统修改状态")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "系统修改状态",path="/api/v1/system/status/{id}/{field}/{value}",code = "L1006",sorted = 6)
    public Boolean updateStatus(
            @PathVariable("id") Long id,
            @PathVariable("field") String field,
            @PathVariable("value") String value
    ) {
        if(StringUtils.isNumeric(value)) {
            return adminSystemService.updateStatus(id, field, Integer.parseInt(value));
        }else{
            return adminSystemService
                    .updateStatusStr(id, field, String.valueOf(value));
        }
    }

    /**
     * 查询用户绑定的系统
     * @param userId
     * @return
     */
    @PostMapping("/system/user/list/{userId}")
    @PermissionApi(title = "查询用户绑定的系统",path="/api/v1/system/user/list/{userId}",code = "L1007",sorted = 7)
    public List<AdminSystemVO> findAdminUserSystem(@PathVariable("userId")Long userId) {
        return adminSystemUserService.findAdminSystemAll(userId);
    }

    /**
     * 绑定用户和系统关系
     *
     * @param adminSystemUserSavePO
     * @return
     */
    @PostMapping("/system/user/bind")
    @ApiLog("系统保存和更新")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "系统保存和更新",path="/api/v1/system/user/bind",code = "L1008",sorted = 8)
    public Boolean bindSystemByUserId(@Validated @RequestBody AdminSystemUserSavePO adminSystemUserSavePO) {
        return adminSystemUserService.bindSystemByUserId(adminSystemUserSavePO.getUserId(),adminSystemUserSavePO.getSystemId());
    }

    /**
     * 解绑绑定用户和系统关系
     *
     * @param adminSystemUserSavePO
     * @return
     */
    @PostMapping("/system/user/unbind")
    @HasAdmin
    @AccessIpLimiter
    @PermissionApi(title = "解绑绑定用户和系统关系",path="/api/v1/system/user/unbind",code = "L1009",sorted = 9)
    public Boolean unbindSystemByUserId(@Validated @RequestBody AdminSystemUserSavePO adminSystemUserSavePO) {
        return adminSystemUserService.unbindSystemByUserId(adminSystemUserSavePO.getUserId(),adminSystemUserSavePO.getSystemId());
    }

    /**
     * 系统切换
     *
     * @param adminSystemLoginVO
     * @return
     */
    @PostMapping("/system/change")
    @PermissionApi(title = "系统切换",path="/api/v1/system/change",code = "L1010",sorted = 10)
    public String systemChange(@RequestBody AdminSystemLoginVO adminSystemLoginVO) {
        return adminSystemService.systemChange(adminSystemLoginVO);
    }

}
